﻿using HaiFengShop.Entity;
using HaiFengShop.ORM;
using HaiFengShop.ORM.Models;
using HaiFengShop.Tools;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;


namespace HaiFengShop.Service
{
    public class AddressService : BaseServer<tb_address>
    {
        private readonly ResponseMessage res;
        private readonly GenericJwtToken genericJwtToken;
        public AddressService(GenericJwtToken genericJwtToken,
            ResponseMessage responseMessage,
             SugarDbContext sugarDbContext) : base(sugarDbContext.Db)
        {
            this.res = responseMessage;
            this.genericJwtToken = genericJwtToken;
        }
        /// <summary>
        /// 保存收货地址
        /// </summary>
        /// <param name="addr"></param>
        /// <returns></returns>
        public ResponseMessage SaveAddress(DTOAddress addr)
        {
            tb_address ta = null;
            if (addr.id == 0)
            {
                ta = new tb_address()
                {
                    name = addr.name,
                    is_delete = 0,
                    mobile = addr.mobile,
                    province_id = addr.province_id,
                    city_id = addr.city_id,
                    district_id = addr.district_id,
                    address = addr.address,
                    user_id = genericJwtToken.Uid,
                    is_default = addr.is_default
                };
                ta.id = InsertReturnIdentity(ta);
            }
            else
            {
                ta = GetSingle(s => s.id == addr.id && s.user_id == genericJwtToken.Uid);
                ta.is_default = addr.is_default;
                Update(ta);
            }
            // 如果设置为默认，则取消其它的默认
            if (addr.is_default == 1)
            {
                var adList = GetList(s => s.id != ta.id && s.user_id == genericJwtToken.Uid);
                adList.ForEach((s) =>
                {
                    s.is_default = 0;
                    Update(s);
                });
            }
            res.data = ta;
            return res;

        }

        /// <summary>
        /// 获取个人详细地址信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseMessage AddressDetail(int id = 0)
        {
            var addressInfo = Context.Queryable<tb_address>()
                .Where(s => s.user_id == genericJwtToken.Uid && s.id == id)
                .First();
            if (addressInfo != null)
            {
                var t_region = Context.Queryable<tb_region>()
                              .In(new int?[] {
                                addressInfo.province_id,
                                addressInfo.city_id,
                            addressInfo.district_id})
                              .OrderBy(s => s.id, OrderByType.Asc)
                              .Select(s => s.name).ToList();
                addressInfo.province_name = t_region[0];
                addressInfo.city_name = t_region[1];
                addressInfo.district_name = t_region[2];
                addressInfo.full_region = string.Join("", t_region);
            }
            res.data = addressInfo == null ? new tb_address() : addressInfo;
            return res;

        }

        /// <summary>
        /// 获取个人收货地址
        /// </summary> 
        public ResponseMessage GetAddresses()
        {

            var addressList = Context.Queryable<tb_address>()
                .Where(s => s.user_id == genericJwtToken.Uid && s.is_delete == 0)
                .OrderBy(s => s.id, OrderByType.Desc)
                .ToList();

            foreach (var addressItem in addressList)
            {
                if (string.IsNullOrEmpty(addressItem.full_region))
                {
                    var t_region = Context.Queryable<tb_region>()
                           .In(new int?[] {
                                addressItem.province_id,
                                addressItem.city_id,
                            addressItem.district_id})
                           .OrderBy(s => s.id, OrderByType.Asc)
                           .Select(s => s.name).ToList();
                    addressItem.province_name = t_region[0];
                    addressItem.city_name = t_region[1];
                    addressItem.district_name = t_region[2];
                    addressItem.full_region = string.Join("", t_region);
                }
            }



            res.data = addressList;
            return res;

        }

        /// <summary>
        /// 删除个人收货地址
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseMessage DeleteAddressById(int id)
        {

            var d = GetSingle(s => s.user_id == genericJwtToken.Uid && s.id == id);
            d.is_delete = 1;
            Update(d);
            res.data = d;
            return res;
        }
    }
}
